package cn.tomoki.demo01.service;

import cn.tomoki.demo01.entity.Appoint;
import cn.tomoki.demo01.entity.Product;
import cn.tomoki.demo01.mapper.AppointMapper;
import cn.tomoki.demo01.mapper.ProductMapper;
import com.sun.javafx.collections.MappingChange;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Service
public class AppointServiceImpl implements AppointService{

    @Autowired
    AppointMapper appointMapper;
    @Autowired
    ProductMapper productMapper;

    /**
     * @author 仓友树
     * @return
     * 查询所有预约记录
     */
    @Override
    public Collection<Appoint> getAppointAll() {
        return appointMapper.getAppointAll();
    }

    /**
     * @auther 仓友树
     * @param mark
     * @return
     * 按流水单返回单个流水单信息以及对应的产品信息
     */
    @Override
    public HashMap<String, Object> getAppointByMark(int mark) {
        Appoint appoint = appointMapper.getAppointByMark(mark);
        Product product = productMapper.getProductByName(appoint.getName());
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("mark",appoint.getMark());
        map.put("userId",appoint.getUserId());
        map.put("productName",appoint.getName());
        map.put("number",appoint.getNumber());
        map.put("time",appoint.getTime());
        map.put("state",appoint.getState());
        map.put("innerWidth",product.getInnerWidth());
        map.put("outerWidth",product.getOuterWidth());
        map.put("tbody",product.getTbody());
        map.put("hole",product.getHole());
        map.put("pbody",product.getPbody());
        map.put("weight",product.getWeight());
        map.put("usable",product.getUsable());
        return map;
    }

    /**
     * @auther 仓友树
     * @param appoint
     * 新增预约
     */
    @Override
    public int addAppoint(Appoint appoint) {
       return appointMapper.insertAppoint(appoint);
    }

    /**
     * @auther 仓友树
     * @param mark
     * 确认收货
     */
    @Override
    public int confirmReceipt(int mark) {
        Map<String,Object> map = new HashMap<String,Object>();
        Appoint appoint = appointMapper.getAppointByMark(mark);
        map.put("mark",appoint.getMark());
        map.put("state",2);
        map.put("name",appoint.getName());
        map.put("appointNumber",appoint.getNumber());
        return appointMapper.updateAppointYes(map);
    }

    /**
     * @auther 仓友树
     * @param mark
     * 取消预约
     */
    @Override
    public int cancelAppoint(int mark) {
        Map<String, Object> map = new HashMap<String, Object>();
        Appoint appoint = appointMapper.getAppointByMark(mark);
        map.put("mark",mark);
        map.put("appointNumber",appoint.getNumber());
        map.put("name",appoint.getName());

        return appointMapper.updateAppointNo(map);
    }
}
